//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Discussions.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;


namespace CommunityServerWeb.ControlPanel.Moderation
{
	/// <summary>
	/// Summary description for TagPosts.
	/// </summary>
	public class TagPosts : BaseModerationPage
	{
		CSContext context = CSContext.Current;
		protected Repeater thePosts;
		protected IPagedControl pager;

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		/// <summary>
		/// The users prefered Grid Page Size, Will return default values before the OnLoad event
		/// Due to dependancy on CSConect.Current.User
		/// </summary>
		public int DefaultPageSize
		{
			get
			{
				return Globals.SafeInt(this.CurrentUser.GetExtendedAttribute("CPPageSize"),10);
			}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			thePosts.ItemCreated +=new RepeaterItemEventHandler(thePosts_ItemCreated);
			JavaScript.RegisterRefresh(this);
			
			if(!IsPostBack)
			{
				string action = Request.QueryString["action"];
				if(action != null)
					HandleActions(action);

				pager.PageSize = Globals.SafeInt(this.CurrentUser.GetExtendedAttribute("CPPageSize"),10);
				SearchResultSet results = CommunityServer.Components.Tags.GetPostsMatchingTags(ApplicationType.Forum, context.Tags, pager.PageIndex, pager.PageSize, true, false);
				pager.TotalRecords = results.TotalRecords;

				thePosts.DataSource = results.Posts;
				thePosts.DataBind();
			}
		}

		private void HandleActions(string action)
		{
			if(action != null && Request.QueryString["postids"] != null)
			{
				action = action.ToLower();
				ArrayList postList = new ArrayList();
				foreach (string sId in Request.QueryString["postids"].Split(','))
				{
					postList.Add(int.Parse(sId));
				}

				int[] postIds = (int[]) postList.ToArray(typeof(int));
				
				switch (action)
				{
					case "remove":
						foreach (int postId in postIds)
						{
							ForumPost post = Posts.GetPost(postId, context.User.UserID, false, false, true);
							if (post.Categories != null && post.Categories.Length > 0)
							{
								ArrayList categories = new ArrayList();
								bool requiresSave = false;
								foreach (string category in post.Categories)
								{
									if (string.Compare(category, context.Tags[0], false) != 0)
										categories.Add(category);
									else
										requiresSave = true;
								}

								if (requiresSave)
								{
									post.Categories = (string[]) categories.ToArray(typeof(string));
									Posts.UpdatePost(post, context.User.UserID);
								}
							}
						}

						break;
				}
			}
		}

		public string GetFilteredUrl()
		{
			string url  = "{0}?Tags={1}";
			url = string.Format(url,Request.Path,Globals.UrlEncodePathComponent(context.Tags[0]));
			return url;
		}

		protected string GetActionUrl(string action, object PostID)
		{
			return GetFilteredUrl() + "&action="+ action + "&postids=" + PostID.ToString();
		}

		private void thePosts_ItemCreated(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
               
			}
		}
	}
}
